<?php
/*
Auth:Sang
Desc:后台功能菜单模型
Date:2014-10-27
*/
namespace Mod;
use Lib\Model;

class MenuUrl extends Model{
	const SESSION_NAME = 'adm_menuurl_list';
	public function getMenuByRole($user_role,$online=1) {
		$url_array = array ();
		$sql ="select * from ".$this->getTableName()." me ,".model('#Module','adm_')->getTableName()." mo where me.menu_id in ($user_role) and me.online=$online and me.module_id = mo.module_id and  mo.online=1";
		 
		$list = $this->db->query($sql)->fetchAll();
		
		if ($list) {
			foreach ( $list as $menu_info ) {
				$url_array [] = $menu_info ['menu_url'];
			}
			return $url_array;
		}
		return array ();
	}

	public function getMenuByUrl($url) {
		$list = $this->where("menu_url='{$url}'")->select();
		if ($list) {
			 $menu= $list[0];
			 $module = model('#Module','adm_')->getModuleById($menu['module_id']);
			 if($module){
				$menu['module_id']=$module['module_id'];
				$menu['module_name']=$module['module_name'];
				$menu['module_url']=$module['module_url'];
				if($menu['father_menu']>0){
					$father_menu=$this->getMenuById($menu['father_menu']);
					$menu['father_menu_url'] = $father_menu['menu_url'];
					$menu['father_menu_name'] = $father_menu['menu_name'];
				}
			}
			return $menu;
		}
		return array ();
	}

	public function getListByModuleId($module_id,$type="all" ) {
		if (! $module_id || ! is_numeric ( $module_id )) {
			return array ();
		}
		$where = "1";
		switch ($type){
			case "sidebar":
				$where.=" and is_show=1 and online=1";
				break;
			case "role":
				$where.=" and online=1";
				break;
			case "navibar":
				$where.=" and is_show=1 and online=1";
				break;
			default:
		}

		$where .= " and module_id={$module_id}";
		$list = $this->where($where)->select();
		if ($list) {
			return $list;
		}
		return array ();
	}

	public function getMenuById($menu_id) {
		if (! $menu_id || ! is_numeric ( $menu_id )) {
			return false;
		}
		$list = $this->where("menu_id='{$menu_id}'")->fetch();
		if ($list) {
			return $list;
		}
		return array ();
	}

	public function getMenuByIds($menu_ids,$online=null,$shortcut_allowed=null) {
		$where="menu_id in({$menu_ids})";
		if(isset($online)){
			$where .= " and online='{$online}'";
		}
		if(isset($shortcut_allowed)){
			$where .= " and shortcut_allowed='{$shortcut_allowed}'";
		}
		
		$list = $this->where($where)->select();
		if ($list) {
			return $list;
		}
		return array ();
 
	}


	/**
	* 批量修改菜单，如批量修改所属模块
	* menu_ids 可以为无key数组，也可以为1,2,3形势的字符串
	*/
	public function batchUpdateMenus($menu_ids,$function_data) {

		if (! $function_data || ! is_array ( $function_data )) {
			return false;
		}
		return $this->where("menu_id in({$menu_ids})")->update($function_data);
	}

	public function updateMenuInfo($menu_id,$function_data) {
		if (! $function_data || ! is_array ( $function_data )) {
			return false;
		}
		return $this->where("menu_id='{$menu_id}'")->update($function_data);
	}


	public function getFatherMenuForOptions() {
		$menu_options_array=array("0"=>"无");
		$modules = model('#Module','adm_')->getAllModules();
		foreach($modules as $module){
			$list = $this->getListByModuleId($module['module_id'],'navibar');
			foreach ($list as $menu){
				$menu_options_array[$module['module_name']][$menu['menu_id']]=$menu['menu_name'];
			}
		}
		return $menu_options_array;
	}
	
	public function addMenu($function_data) {
		if (! $function_data || ! is_array ( $function_data )) {
			return false;
		}
		$this->clearSession();
		return $this->insert($function_data);
	}
	
	public function getAllMenus($start =0 ,$page_size=20) {
		$result = $this->limit($start,$page_size)->select();
		$session_list = $this->getSessionMenus();
		foreach($result as &$menu){
			if($menu['father_menu']>0){
				$menu['father_menu_name'] = $session_list[$menu['father_menu']]['menu_name'];
			}
		}
		return $result;
	}
	
	public function clearSession(){
		session(self::SESSION_NAME,null);
	}
	
	public function getSessionMenus() {
		if(array_key_exists(self::SESSION_NAME,$_SESSION)){
			return $_SESSION[self::SESSION_NAME];
		}else{
			$result = $this->select();
			$new_list=array();
			foreach($result as $menu){
				$new_list[$menu['menu_id']] = $menu;
			}
			unset($result);
			foreach($new_list as $menu_id =>&$menu){
				if($menu['father_menu']>0){
					$menu['father_menu_name'] = $new_list[$menu['father_menu']]['menu_name'];
				}
			}
			if ($new_list) {
				session(self::SESSION_NAME, $new_list);
			}
			return $new_list;
		}	
	}
	
	public function search($module_id,$menu_name,$start=0,$page_size=20) {
		$limit ="";
		$where = " where 1";
		if($page_size){
			$limit ="limit $start,$page_size";
		}

		if($module_id>0){
			$where .= " and me.module_id=$module_id ";
		}
		if($menu_name!=""){
			$where .= " and me.menu_name like '%$menu_name%' ";
		}

		$sql = "select * ,coalesce(mo.module_name,'已删除') from ".$this->getTableName()." me left join ".model('#Module','adm_')->getTableName()." mo on me.module_id = mo.module_id $where order by me.module_id,me.menu_id $limit";
		$list=$this->db->query($sql)->fetchAll();
		$session_list = $this->getSessionMenus();
	
		if ($list) {
			foreach($list as &$menu){
				if($menu['father_menu']>0){
					$menu['father_menu_name'] = $session_list[$menu['father_menu']]['menu_name'];
				}
			}
			return $list;
		}
		return array ();
	}

	public function searchCount($module_id,$menu_name) {
		$where = '1';
		if($module_id>0){
			$where .= " and module_id='{$module_id}'";
		}
		if($menu_name!=""){
			$where .= " and menu_name like '%$menu_name%'";
		}
		$num = $this->where($where)->count();
		return $num ? $num : 0;
	}
	
	public function delMenu($menu_id) {
		if (! $menu_id || ! is_numeric ( $menu_id )) {
			return false;
		}
		return $this->where("menu_id={$menu_id}")->delete();
	}
	
}